package com.yang.controller;



import com.yang.annotation.OptLogger;
import com.yang.domain.dto.Result;
import com.yang.limit.aop.Limit;
import com.yang.service.IVoucherOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import static com.yang.constants.OptTypeConstants.ADD;
import static com.yang.enums.SeckillTypeEnum.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang
 * @since 2022-10-22
 */
@Api(value = "订单模块")
@RestController
@RequestMapping("/voucher-order")
public class VoucherOrderController {

    @Resource
    private IVoucherOrderService voucherOrderServiceMq;

    /**
     * 秒杀券的订单信息
     * @param voucherId 订单id
     * @return 秒杀结果
     */
    @PostMapping("seckill/{id}")
    @OptLogger(value = ADD)
    @Limit(key = "seckill:")
    @ApiOperation(value = "秒杀券的订单信息")
    public Result seckillVoucher(@PathVariable("id") Long voucherId) {

        Long orderId = voucherOrderServiceMq.seckillVoucher(voucherId);

        if (SECKILL_ERROR.getCode().equals(orderId)){
            return Result.fail(SECKILL_ERROR.getMag());
        }
        if (STOCK_NULL.getCode().equals(orderId)){
            return Result.fail(STOCK_NULL.getMag());
        }
        if (REPEAT_ORDER.getCode().equals(orderId)){
            return Result.fail(REPEAT_ORDER.getMag());
        }

        return Result.ok(orderId);
    }



}
